﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using Lib.Helper;
using MSIACCSL2.InventoryServices;
using MSIACCSL2.WarehouseServices;
using WcfService1.DTO;

namespace MSIACCSL2.ViewModels
{
    public class InventoryWarehouseViewModel : ViewModelBase
    {
        private readonly InventoryServiceClient client;
        private InventoryDTO _Inventory;
        private ObservableCollection<InventoryWarehouseDTO> _InventoryWarehouses;

        public InventoryWarehouseViewModel()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                client = new InventoryServiceClient();
            }

            EventAggregator.RegisterMessage("LoadInventoryWarehouse", (x) =>
                                                                          {
                                                                              GetInventoryWarehouses(x as InventoryDTO);
                                                                              _Inventory = x as InventoryDTO;
                                                                          });
        }


        public ObservableCollection<InventoryWarehouseDTO> InventoryWarehouses
        {
            get { return _InventoryWarehouses; }
            private set
            {
                if (InventoryWarehouses == value) return;
                _InventoryWarehouses = value;
                FirePropertyChanged("InventoryWarehouses");
            }
        }


        public void GetInventoryWarehouses(InventoryDTO inventory)
        {
            client.GetInventoryWarehouseListCompleted += client_GetInventoryWarehouseListCompleted;
            client.GetInventoryWarehouseListAsync(inventory.SerialNumber);
        }

        private void client_GetInventoryWarehouseListCompleted(object sender,
                                                               GetInventoryWarehouseListCompletedEventArgs e)
        {
            client.GetInventoryWarehouseListCompleted -= client_GetInventoryWarehouseListCompleted;
            InventoryWarehouses = e.Result;
            EventAggregator.PublishMessage("RefreshDataContext", this);
        }

        public void SaveInventoryWarehouse(Object param)
        {
            InventoryWarehouses = new ObservableCollection<InventoryWarehouseDTO>();
            foreach (WarehouseDTO warehouse in (param as ObservableCollection<WarehouseDTO>))
            {
                InventoryWarehouses.Add(new InventoryWarehouseDTO
                                            {
                                                Inventory = _Inventory,
                                                Warehouse = warehouse,
                                                Qty = warehouse.Qty,
                                                Sheet = warehouse.Sheet
                                            });
            }
            client.SaveInventoryWarehouseCompleted += client_SaveInventoryWarehouseCompleted;
            client.SaveInventoryWarehouseAsync(InventoryWarehouses);
        }

        private void client_SaveInventoryWarehouseCompleted(object sender, SaveInventoryWarehouseCompletedEventArgs e)
        {
            client.SaveInventoryWarehouseCompleted += client_SaveInventoryWarehouseCompleted;
            MessageBox.Show(e.Result.ToString());
        }


        public void AddInventoryWarehouse(Object param)
        {
            InventoryWarehouses.Add(new InventoryWarehouseDTO());
        }

        public void DeleteInventoryWarehouse(Object param)
        {
            InventoryWarehouses.Remove(param as InventoryWarehouseDTO);
        }
    }

    public class InventoryWarehouseWrapViewModel : ViewModelBase
    {
        private readonly WarehouseServicesClient WarehouseClient;
        private ObservableCollection<WarehouseDTO> _warehouses;

        public InventoryWarehouseWrapViewModel()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                WarehouseClient = new WarehouseServicesClient();
                GetWarehouseList();
            }
        }

        public ObservableCollection<WarehouseDTO> Warehouses
        {
            get { return _warehouses; }
            private set
            {
                if (Warehouses == value) return;
                _warehouses = value;
                FirePropertyChanged("Warehouses");
            }
        }

        public void GetWarehouseList()
        {
            WarehouseClient.GetAllWarehouseCompleted += WarehouseClient_GetAllWarehouseCompleted;
            WarehouseClient.GetAllWarehouseAsync();
        }

        private void WarehouseClient_GetAllWarehouseCompleted(object sender, GetAllWarehouseCompletedEventArgs e)
        {
            WarehouseClient.GetAllWarehouseCompleted -= WarehouseClient_GetAllWarehouseCompleted;
            Warehouses = e.Result;
        }
    }
}